<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function minimalKSum(nums, k) {
      nums = [...new Set(nums)].sort((a, b) => a - b);
      const n = nums.length;
      let exist = 0;
      for (let i = 0; i < n; i++) {
        if (nums[i] <= k) {
          k++;
          exist += nums[i];
        } else {
          break;
        }
      }

      return k * (k + 1) / 2 - exist;
    };
    console.log(minimalKSum(nums = [1, 4, 25, 10, 25], k = 2))
  </script>
</body>

</html>